home *** CD-ROM | disk | FTP | other *** search
/ Programming an RTS Game with Direct3D / Programming an RTS Game with Direct3D.iso / Examples / Chapter 12 / Example 12.5 / mouse.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2006-07-16  |  5.0 KB  |  209 lines

  1. #include "mouse.h"
  2.  
  3. //////////////////////////////////////////////////////////////////////////
  4. //                        RAY                                                //
  5. //////////////////////////////////////////////////////////////////////////
  6.  
  7. RAY::RAY()
  8. {
  9.     org = dir = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
  10. }
  11.  
  12. RAY::RAY(D3DXVECTOR3 o, D3DXVECTOR3 d)
  13. {
  14.     org = o;
  15.     dir = d;
  16. }
  17.  
  18. float RAY::Intersect(MESHINSTANCE iMesh)
  19. {
  20.     if(iMesh.m_pMesh == NULL)return -1.0f;
  21.     return Intersect(iMesh.m_pMesh->m_pMesh);
  22. }
  23.  
  24. float RAY::Intersect(BBOX bBox)
  25. {
  26.     if(D3DXBoxBoundProbe(&bBox.min, &bBox.max, &org, &dir))
  27.         return D3DXVec3Length(&(((bBox.min + bBox.max) / 2.0f) - org));
  28.     else return -1.0f;
  29. }
  30.  
  31. float RAY::Intersect(BSPHERE bSphere)
  32. {
  33.     if(D3DXSphereBoundProbe(&bSphere.center, bSphere.radius, &org, &dir))
  34.         return D3DXVec3Length(&(bSphere.center - org));
  35.     else return -1.0f;
  36. }
  37.  
  38. float RAY::Intersect(ID3DXMesh* mesh)
  39. {
  40.     if(mesh == NULL)return -1.0f;
  41.  
  42.     // Collect only the closest intersection
  43.     BOOL hit;
  44.     DWORD dwFace;
  45.     float hitU, hitV, dist;
  46.     D3DXIntersect(mesh, &org, &dir, &hit, &dwFace, &hitU, &hitV, &dist, NULL, NULL);
  47.  
  48.     if(hit) return dist;
  49.     else return -1.0f;
  50. }
  51.  
  52. //////////////////////////////////////////////////////////////////////////
  53. //                        MOUSE                                            //
  54. //////////////////////////////////////////////////////////////////////////
  55.  
  56. MOUSE::MOUSE()
  57. {
  58.     m_pMouseTexture = NULL;
  59.     m_pMouseDevice = NULL;
  60. }
  61.  
  62. MOUSE::~MOUSE()
  63. {
  64.     if(m_pMouseDevice != NULL)
  65.         m_pMouseDevice->Release();
  66.  
  67.     if(m_pMouseTexture != NULL)
  68.         m_pMouseTexture->Release();
  69. }
  70.  
  71. void MOUSE::InitMouse(IDirect3DDevice9* Dev, HWND wnd)
  72. {
  73.     try
  74.     {
  75.         m_pDevice = Dev;
  76.  
  77.         //Load texture and init sprite
  78.         D3DXCreateTextureFromFile(m_pDevice, "cursor/cursor.dds", &m_pMouseTexture);
  79.         D3DXCreateSprite(m_pDevice, &m_pSprite);
  80.  
  81.         //Get directinput object
  82.         LPDIRECTINPUT8 directInput = NULL;
  83.  
  84.         DirectInput8Create(GetModuleHandle(NULL),    // Retrieves the application handle
  85.                            0x0800,                    // v.8.0    
  86.                            IID_IDirectInput8,        // Interface ID
  87.                            (VOID**)&directInput,    // Our DirectInput Object
  88.                            NULL);
  89.  
  90.         //Acquire Default System mouse
  91.         directInput->CreateDevice(GUID_SysMouse, &m_pMouseDevice, NULL);        
  92.         m_pMouseDevice->SetDataFormat(&c_dfDIMouse);
  93.         m_pMouseDevice->SetCooperativeLevel(wnd, DISCL_EXCLUSIVE | DISCL_FOREGROUND);
  94.         m_pMouseDevice->Acquire();            
  95.  
  96.         //Get viewport size and init mouse location
  97.         D3DVIEWPORT9 v;
  98.         m_pDevice->GetViewport(&v);
  99.         m_viewport.left = v.X;
  100.         m_viewport.top = v.Y;
  101.         m_viewport.right = v.X + v.Width;
  102.         m_viewport.bottom = v.Y + v.Height;
  103.  
  104.         x = v.X + v.Width / 2.0f;
  105.         y = v.Y + v.Height / 2.0f;
  106.  
  107.         //Release Direct Input Object
  108.         directInput->Release();
  109.     }
  110.     catch(...)
  111.     {
  112.         debug.Print("Error in MOUSE::InitMouse()");
  113.     }    
  114. }
  115.  
  116. bool MOUSE::ClickLeft()
  117.     return m_mouseState.rgbButtons[0];
  118. }
  119.  
  120. bool MOUSE::ClickRight()
  121. {
  122.     return m_mouseState.rgbButtons[1];
  123. }
  124.  
  125. bool MOUSE::WheelUp()
  126. {
  127.     return m_mouseState.lZ > 0.0f;
  128. }
  129.  
  130. bool MOUSE::WheelDown()
  131. {
  132.     return m_mouseState.lZ < 0.0f;
  133. }
  134.  
  135. bool MOUSE::Over(RECT dest)
  136. {
  137.     if(x < dest.left || x > dest.right)return false;
  138.     if(y < dest.top || y > dest.bottom)return false;
  139.     return true;
  140. }
  141.  
  142. bool MOUSE::PressInRect(RECT dest)
  143. {
  144.     return ClickLeft() && Over(dest);
  145. }
  146.  
  147. void MOUSE::Update()
  148. {
  149.     //Retrieve mouse state
  150.     ZeroMemory(&m_mouseState, sizeof(DIMOUSESTATE));
  151.     m_pMouseDevice->GetDeviceState(sizeof(DIMOUSESTATE), &m_mouseState);
  152.  
  153.     //Update pointer
  154.     x += m_mouseState.lX * 1.0f;
  155.     y += m_mouseState.lY * 1.0f;
  156.  
  157.     //Keep mouse pointer within window
  158.     if(x < m_viewport.left)    x = m_viewport.left;
  159.     if(y < m_viewport.top)    y = m_viewport.top;
  160.     if(x > m_viewport.right)    x = m_viewport.right;
  161.     if(y > m_viewport.bottom)    y = m_viewport.bottom;
  162. }
  163.  
  164. void MOUSE::Paint()
  165. {    
  166.     if(m_pMouseTexture != NULL)
  167.     {
  168.         RECT src = {0,0,20,20};
  169.  
  170.         m_pSprite->Begin(D3DXSPRITE_ALPHABLEND);
  171.         m_pSprite->Draw(m_pMouseTexture, &src, NULL, &D3DXVECTOR3(x, y, 0.0f), 0xffffffff);
  172.         m_pSprite->End();
  173.     }    
  174. }
  175.  
  176. RAY MOUSE::GetRay()
  177. {
  178.     try
  179.     {
  180.         D3DXMATRIX projectionMatrix, viewMatrix, worldViewInverse, worldMatrix;
  181.         
  182.         m_pDevice->GetTransform(D3DTS_PROJECTION, &projectionMatrix);
  183.         m_pDevice->GetTransform(D3DTS_VIEW, &viewMatrix);
  184.         m_pDevice->GetTransform(D3DTS_WORLD, &worldMatrix);
  185.  
  186.         float width = m_viewport.right - m_viewport.left;
  187.         float height = m_viewport.bottom - m_viewport.top;
  188.  
  189.         float angle_x = (((2.0f * x) / width) - 1.0f) / projectionMatrix(0,0);
  190.         float angle_y = (((-2.0f * y) / height) + 1.0f) / projectionMatrix(1,1); 
  191.         
  192.         RAY ray;
  193.         ray.org = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
  194.         ray.dir = D3DXVECTOR3(angle_x, angle_y, 1.0f);
  195.  
  196.         D3DXMATRIX m = worldMatrix * viewMatrix;
  197.         D3DXMatrixInverse(&worldViewInverse, 0, &m);
  198.         D3DXVec3TransformCoord(&ray.org, &ray.org, &worldViewInverse);
  199.         D3DXVec3TransformNormal(&ray.dir, &ray.dir, &worldViewInverse);
  200.         D3DXVec3Normalize(&ray.dir, &ray.dir);
  201.  
  202.         return ray;
  203.     }
  204.     catch(...)
  205.     {
  206.         debug.Print("Error in MOUSE::GetRay()");
  207.     }
  208. }